package rcos.rpibuckhunter.archersfriend;

import java.util.concurrent.TimeUnit;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class RoundTimer extends Activity implements OnClickListener {
	private TextView roundTimer;
	private ArcheryCountDownTimer countDown;
	private PrepCountDownTimer prepCountDown;
	private Button startRoundButton;
	
	private final long startTime = 121000;
	private final long interval = 1000;
	private final long prepTime = 10000;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_round_timer);
        startRoundButton = (Button)this.findViewById(R.id.button1);
        startRoundButton.setOnClickListener(this);
        
        roundTimer = (TextView) this.findViewById(R.id.textView1);
        prepCountDown = new PrepCountDownTimer (prepTime, interval);
        countDown = new ArcheryCountDownTimer (startTime, interval);
        roundTimer.setText(String.format("%02d", TimeUnit.MILLISECONDS.toSeconds(prepTime)-
        		TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(prepTime))));
        
    }
    
	@Override
	public void onClick(View v) {
		startRoundButton.setVisibility(View.INVISIBLE);
		View someView = findViewById(R.id.textView1);
		View root = someView.getRootView();
		root.setBackgroundColor(getResources().getColor(R.color.red));
		prepCountDown.start();	
	}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

	
	public class ArcheryCountDownTimer extends CountDownTimer {

		public ArcheryCountDownTimer(long startTime, long interval) {
			super(startTime, interval);
		}

		@Override
		public void onFinish() {
			finishActivity(0);
			startActivity(new Intent(RoundTimer.this, ScoreKeeper.class));	
		}

		@Override
		public void onTick(long millisUntilFinished) {
			if (TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) == 30) {
				View someView = findViewById(R.id.textView1);
				View root = someView.getRootView();
				root.setBackgroundColor(getResources().getColor(R.color.yellow));
			}
			if (TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) == 10) {
				View someView = findViewById(R.id.textView1);
				View root = someView.getRootView();
				root.setBackgroundColor(getResources().getColor(R.color.red));
			}
	        roundTimer.setText(String.format("%d:%02d", TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished), 
	        		TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished)-
	        		TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));		
		}
	}
	
	public class PrepCountDownTimer extends CountDownTimer {

		public PrepCountDownTimer(long prepTime, long interval) {
			super(prepTime, interval);
		}

		@Override
		public void onFinish() {
			roundTimer.setText(String.format("%d:%02d", TimeUnit.MILLISECONDS.toMinutes(startTime), 
	        		TimeUnit.MILLISECONDS.toSeconds(startTime)-
	        		TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(startTime))));
			View someView = findViewById(R.id.textView1);
			View root = someView.getRootView();
			root.setBackgroundColor(getResources().getColor(R.color.green));
			countDown.start();
		}

		@Override
		public void onTick(long millisUntilFinished) {
			roundTimer.setText(String.format("%02d", TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished)-
	        		TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));
		}
	}
}
